// pages/optional/components/selectbox/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    selectedIds: {
      type: Array,
      value: []
    },
    selectedList: {
      type: Array,
      value: []
    },
    planMatchId:{
      type:String,
      value:''
    },
    mode:{
      type:String,
      value:'school'
    },
    props:{
      type:Object,
      value:{
        name:'title',
        id:'id'
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    visible: false,
  },


  /**
   * 组件的方法列表
   */
  methods: {
    toggleModal() {
      this.setData({
        visible: !this.data.visible
      })
    },
    confirm() {
      const {
        selectedList,
        mode,
        selectedIds,
        subjects,
        planMatchId
      } = this.data
      const optionalFilters=wx.getStorageSync('optionalChooseFilters')
      if(selectedList.length<1){
        // console.log(mode)
        wx.showToast({
          title: '至少选择一个'+(mode=='school'?'学校':'专业'),
          icon:'none'
        })
        return
      }
      const params={
        selectedList,
        mode,
        selectedIds,
        year:optionalFilters.year,
        provinceId:optionalFilters.province
      }
      if(planMatchId!==''){
        params.planMatchId=planMatchId
      }
      this.triggerEvent('confirm')
      wx.navigateTo({
        url: '/pages/optional/report/index?params=' + encodeURIComponent(JSON.stringify(params))
      })
    },
    clearAll(){
      this.setData({
        selectedList:[],
        selectedIds:[]
      })
      this.triggerEvent('onChange',{selectedList:[],selectedIds:[]})
    },
    clearItem(e){
      const id=e.currentTarget.dataset.id
      const {selectedIds,selectedList,props}=this.data
      selectedList.map((item,index)=>{
        if(item[props.id]==id){
          selectedList.splice(index,1)
        }
      })
      selectedIds.splice(selectedIds.indexOf(id),1)
      this.setData({
        selectedList,
        selectedIds
      })
      this.triggerEvent('onChange',{selectedList,selectedIds,id})
    }
  }
})